﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCS.Core.Models;
using System.Linq.Expressions;
using SCSService;
using SCS.Core.ViewModels;
using System.Collections;

namespace SCS.Core.Controllers
{
    public class PostArticleController : BaseController
    {
        public ActionResult Index()
        {
            return Redirect("ListPostArticle");
        }

        #region View ListPostArticle
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult SaveEditPostArticle()
        {
            if (Session["UserID"] == null)
            {
                return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/User/SignIn");
            }
            var lPostArticle = new Models.Collection.PostArticleCollection();
            TryUpdateModel(lPostArticle);
            Post GetPostArticle = new Post();
            GetPostArticle.PostID = lPostArticle.PostID;
            GetPostArticle.PostTitle = lPostArticle.PostTitle;
            GetPostArticle.PostContent = lPostArticle.PostContent;
            GetPostArticle.PostAbstract = lPostArticle.PostAbstract;
          
            GetPostArticle.PostCreat = lPostArticle.PostCreat;
            GetPostArticle.ModifyDate = lPostArticle.ModifyDate;
         
            GetPostArticle.MetaDesciption = lPostArticle.MetaDesciption;
            GetPostArticle.PostImage = lPostArticle.Images;
            GetPostArticle.PostSource = lPostArticle.PostSource;
            GetPostArticle.Title = lPostArticle.Title;
            int iPostId = Models.PostArticleModel.UpdatePostArticle(GetPostArticle);
            //update lại các danh mục category
            if (lPostArticle.ArrayCategoryID != "")
            {
                //Delete all Product in Type_Product_Manage
                Models.PostArticleModel.DeletePost_In_CategoryByPostID(iPostId);

                //Create Product Type Manager New
                var arr = lPostArticle.ArrayCategoryID.Split(',');
                foreach (var item in arr)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        //Add Product to Type_Product_Manage
                        SCSService.Post_In_Category Post_In_CategorySvc = new Post_In_Category();
                        Post_In_CategorySvc.PostID = iPostId;
                        Post_In_CategorySvc.CategoryID = int.Parse(item);
                        Post_In_CategorySvc.CreateDate = DateTime.Now;
                        Models.PostArticleModel.iCreatePost_In_Category(Post_In_CategorySvc);
                    }
                }
            }

            return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/PostArticle/ListPostArticle/");
        }
        public ActionResult EditPostArticle(int? id)
        {
            int siteGuid = GetSiteGuid();
            //lấy về danh sách các CatagoryID theo PostId 
           
            int languageId = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());
            if (Session["UserID"] == null)
            {
                return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/User/SignIn");
            }
            Models.Collection.PostArticleCollection PostArticle = new Models.Collection.PostArticleCollection();
            Post GetPostArticle = PostArticleModel.GetPostByPostId(id);
            PostArticle.PostID = GetPostArticle.PostID;
            PostArticle.PostTitle = GetPostArticle.PostTitle;
            PostArticle.PostContent = GetPostArticle.PostContent;
            PostArticle.PostAbstract = GetPostArticle.PostAbstract;
            PostArticle.PostStatus = (int)GetPostArticle.PostStatus;
            PostArticle.PostCreat = GetPostArticle.PostCreat;
            PostArticle.ModifyDate = GetPostArticle.ModifyDate;
       
            PostArticle.MetaDesciption = GetPostArticle.MetaDesciption;
            PostArticle.Images = GetPostArticle.PostImage;
            PostArticle.PostSource = GetPostArticle.PostSource;
            PostArticle.Title = GetPostArticle.Title;
            var ListCategoryId = PostArticleModel.lGetCategoryIDByPostID(id);
            string arrId = "";
            foreach (var item in ListCategoryId)
            {
                arrId += item + ",";
            }
            PostArticle.ArrayCategoryID = arrId.Length > 1 ? arrId.Substring(0, arrId.LastIndexOf(',')) : "";
            ViewData["IsAdd"] = false;
            ViewData["ListPostArticleCategoryDrop"] = new SelectList(PostArticleModel.lGetAllCatePostArticle(languageId,siteGuid), "CategoryID", "CategoryName");
            return View("DetailPostArticle", PostArticle);
        
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ListPostArticle(int? iCategoryId,int? iPostUser)
        {
            int siteGuid = GetSiteGuid();
            int languageId = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());

            if (Session["UserID"] == null)
            {
                return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/User/SignIn");
            }
            else
            return ListPostArticle(iCategoryId, iPostUser, siteGuid, languageId);
        }
        public ActionResult ListPostArticle(int? iCategoryId,int? iPostUser, int iSiteGuid,int iLanguageId )
        {
            try
            {
                if (Session["UserID"] == null)
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/User/SignIn");
                }
                List<Post> IPostArticles = PostArticleModel.IGetListPostByIdCate(iCategoryId, iPostUser, iSiteGuid, iLanguageId);

                ListPostArticleViewModels lPostArticle = new ListPostArticleViewModels();

                if (IPostArticles != null)
                {
                    lPostArticle.lPosts = IPostArticles;
                    return View(lPostArticle);
                }
                else
                {
                    return View(lPostArticle);
                }
            }
            catch
            {
                return View("ListPostArticle",new ListPostArticleViewModels());
            }

        }
        public ActionResult ChangeStatus(string id)
        {
            string[] getId = id.Split(',');
            int PostID =int.Parse( getId[0]);
            int Values =int.Parse(getId[1]);
            Models.PostArticleModel.UpdateStatus(PostID, Values);

            string outStr = "" + Values;
            return Json(outStr);
        }
        public ActionResult DeletePost(int? id, string returnUrl)
        {
            PostArticleModel.DeletePost(id);
            return Redirect(returnUrl);
        }
        #endregion
    }
}
